import { bindable, containerless } from 'aurelia-framework';

@containerless
export class EmProjectEdit {

    users = nsCtx.users;
    loginUser = nsCtx.loginUser;

    project;

    /**
     * 当视图被附加到DOM中时被调用
     */
    attached() {
        $(this.tabRef).find('.item').tab({
            onVisible: (tabPath) => {
                // console.log(tabPath);
                ea.publish(nsCons.EVENT_TASK_PROJECT_EDIT_TAB_CHANGED, {
                    tab: tabPath,
                });
            }
        });
    }

    show(opt) {
        let p = {};
        p.id = opt.project.id;
        p.name = opt.project.name;
        p.key = opt.project.key;
        p.website = opt.project.website;
        p.leader = opt.project.leader.username;
        p.operator = opt.project.operator.username;
        p.members = opt.project.members;

        this.project = p;
        this.emModal.show({
            hideOnApprove: true,
            autoDimmer: false
        });
    }


    showHandler() {}

    saveHandler() {
        this.emModal.showDimmer();

        $.post('/admin/task/project/update', {
            id: this.project.id,
            name: this.project.name,
            website: this.project.website,
            leader: this.project.leader,
            operator: this.project.operator,
        }, (data) => {
            this.emModal.hideDimmer();
            if (data.success) {
                toastr.success('更新项目成功!');
                ea.publish(nsCons.EVENT_TASK_PROJECT_UPDATED, {
                    channel: data.data
                });
            } else {
                toastr.error(data.data, '更新项目失败!');
            }
        });
    }
    approveHandler(modal) {

    }
}
